package com.zqx.saller.service;

import com.zqx.api.ProductRpc;
import com.zqx.api.domain.ProductRpcReq;
import com.zqx.entity.Product;
import com.zqx.entity.enums.ProductStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ProductRpcService {

    @Resource
    private ProductRpc productRpc;

    @Autowired
    private ProductCache productCache;
    /**
     * 查询全部产品
     * @return
     */
    public List<Product> findAll(){
        ProductRpcReq req = new ProductRpcReq();
        List<String> status = new ArrayList<>();
        status.add(ProductStatus.IN_SELL.name());
        Pageable pageable = new PageRequest(0, 1000, Sort.Direction.DESC,"rewardRate");
        req.setStatusList(status);
        /*req.setPage(0);
        req.setPageSize(1000);
        req.setDirection(Sort.Direction.DESC);
        req.setOrderBy("rewardRate");*/
        //log.info("rpc查询全部产品,请求:{}",req);
        List<Product> result = productRpc.query(req);
        //log.info("rpc查询全部产品,结果:{}",result);
        return result;
    }

    /**
     * 查询单个产品
     * @param id
     * @return
     */
    public Product findOne(String id){
        Product product = productCache.readCache(id);
        if (product == null){
            productCache.readCache(id);
        }
        return product;
    }

    //@PostConstruct
    //public void testFindAll(){
        //findAll();
    //}
}
